package com.tj.ocr.img;

import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;

import javax.imageio.IIOImage;
import javax.imageio.ImageIO;
import javax.imageio.ImageWriteParam;
import javax.imageio.ImageWriter;

public class ImageRW {
	public static BufferedImage read(String path){
		try {
			return ImageIO.read(new File(path));
		} catch (IOException e) {
			System.err.println(path);
			e.printStackTrace();
		}
		
		return null;
	}
	
	public static void write(BufferedImage image,String path){
		try {
			if(path.endsWith("jpg"))
				ImageIO.write(image, "jpeg", new File(path));
			else if(path.endsWith("png"))
				ImageIO.write(image, "png", new File(path));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public static void get1stLine(BufferedImage img){
		int w = img.getWidth();
		System.out.print("R-");
		for(int i=0;i<w;i++){
			System.out.print(getVal(img.getRGB(i, 0),0)+",");
		}
		System.out.println();
		
		System.out.print("G-");
		for(int i=0;i<w;i++){
			System.out.print(getVal(img.getRGB(i, 0),1)+",");
		}
		System.out.println();
		
		System.out.print("B-");
		for(int i=0;i<w;i++){
			System.out.print(getVal(img.getRGB(i, 0),2)+",");
		}
		System.out.println();
	}
	
	public static void get1stLineEx(BufferedImage img){
		int w = img.getHeight();
		System.out.print("R-");
		for(int i=0;i<w;i++){
			System.out.print(getVal(img.getRGB(1, i),0)+",");
		}
		System.out.println();
		
		System.out.print("G-");
		for(int i=0;i<w;i++){
			System.out.print(getVal(img.getRGB(1, i),1)+",");
		}
		System.out.println();
		
		System.out.print("B-");
		for(int i=0;i<w;i++){
			System.out.print(getVal(img.getRGB(1, i),2)+",");
		}
		System.out.println();
	}
	
	public static int getVal(int pixel,int tp){
		int ret = 0;
		
		if(tp == 0)
			ret = (pixel>>16) & 0xff;
		else if(tp == 1)
			ret = (pixel>>8) & 0xff;
		else if(tp == 2)
			ret = (pixel & 0xff);
		
		return ret;
	}
	
	public static byte[] bufferedImageTobytes(BufferedImage image, float quality) {  
        System.out.println("jpeg" + quality + "质量开始打包");  
        // 如果图片空，返回空  
        if (image == null) {  
            return null;  
        }     
        // 得到指定Format图片的writer  
        Iterator<ImageWriter> iter = ImageIO  
                .getImageWritersByFormatName("jpeg");// 得到迭代器  
        ImageWriter writer = (ImageWriter) iter.next(); // 得到writer  
  
        // 得到指定writer的输出参数设置(ImageWriteParam )  
        ImageWriteParam iwp = writer.getDefaultWriteParam();  
        iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); // 设置可否压缩  
        iwp.setCompressionQuality(quality); // 设置压缩质量参数  
  
        iwp.setProgressiveMode(ImageWriteParam.MODE_DISABLED);  
  
        ColorModel colorModel = ColorModel.getRGBdefault();  
        // 指定压缩时使用的色彩模式  
        iwp.setDestinationType(new javax.imageio.ImageTypeSpecifier(colorModel,  
                colorModel.createCompatibleSampleModel(16, 16)));  
  
        // 开始打包图片，写入byte[]  
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); // 取得内存输出流  
        IIOImage iIamge = new IIOImage(image, null, null);  
        try {  
            // 此处因为ImageWriter中用来接收write信息的output要求必须是ImageOutput  
            // 通过ImageIo中的静态方法，得到byteArrayOutputStream的ImageOutput  
            writer.setOutput(ImageIO  
                    .createImageOutputStream(byteArrayOutputStream));  
            writer.write(null, iIamge, iwp);  
        } catch (IOException e) {  
            System.out.println("write errro");  
            e.printStackTrace();  
        }  
        System.out.println("jpeg" + quality + "质量完成打包-----"
                + "----lenth----" + byteArrayOutputStream.toByteArray().length);  
        return byteArrayOutputStream.toByteArray();  
    }  
	
	public static void main(String[] args){
		get1stLine(read("c:/1.jpg"));
	}
}
